﻿namespace com.stifel.ServerBuildMgmt.BuildMgmt1
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml.Linq;

    using com.stifel.Extensions.ObjectExtensions;
    using com.stifel.Extensions.SharePointExtensions;
    using com.stifel.Extensions.StringExtensions;
    using com.stifel.ServerBuildMgmtDao;

    using Microsoft.SharePoint;
    using com.stifel.SharePoint;

    public partial class BuildMgmt1UserControl : UserControl
    {
        #region Methods

        protected void AddUserAndRole_Click( object sender, ImageClickEventArgs e )
        {
            AddPanel objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
            if ( ( uiRoles.SelectedItem.Text == string.Empty ) && ( uiUsers.SelectedItem.Text == string.Empty ) )
            {
                summarize( );
            }
            else
            {
                ServerValues val = objAddPanel.currentUIContentValueV;
                Access Access = new Access( );
                if ( uiUsers.SelectedIndex > -1 )
                {
                    Access.User = uiUsers.SelectedItem.Text;
                    val.accessUserRoleColl.Add( uiUsers.SelectedItem.Text );
                    uiUsers.Items.Remove( uiUsers.SelectedItem );
                }
                if ( uiRoles.SelectedIndex > -1 )
                {
                    Access.Role = uiRoles.SelectedItem.Text;
                }
                val.Access.Add( Access );
                accessRepeater.DataSource = val.Access;
                accessRepeater.DataBind( );
                uiRolesForAccess_Load( );
                summarize( objAddPanel.accessArg );
            }
            uiObjAddPanel.Value = objAddPanel.SerializeToXml<AddPanel>( ).ToBase64String( );
        }

        protected void onItemCommand( object sender, RepeaterCommandEventArgs e )
        {
            SetRequestPanel( );
            AddPanel objAddPanel;
            if ( e.CommandName == "Delete" )
            {
                isChanged.Value = true.ToString( );
                int delete = 0;
                objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
                setDeleteRepeaterBindingR( e.CommandArgument.ToString( ) );

                objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
                objAddPanel.currentRepeaterBindingR = objAddPanel.referenceRepeaterBindingR;
                uiObjAddPanel.Value = objAddPanel.SerializeToXml<AddPanel>( ).ToBase64String( );

                objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
                objAddPanel.currentUIContentValueV = getValueAsObject( objAddPanel.currentRepeaterBindingR.value );
                uiObjAddPanel.Value = objAddPanel.SerializeToXml<AddPanel>( ).ToBase64String( );
                objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
                for ( int i = 0; i < objAddPanel.repeaterBinding.Count; i++ )
                {
                    if ( objAddPanel.currentRepeaterBindingR.value.FromBase64String( ).Equals( objAddPanel.repeaterBinding[ i ].value.FromBase64String( ) ) )
                    {
                        delete = i;
                    }
                }
                objAddPanel.repeaterBinding.Remove( objAddPanel.repeaterBinding[ delete ] );
                myRepeater.DataSource = objAddPanel.repeaterBinding;
                myRepeater.DataBind( );
                isSomethingChanged( );
                uiObjAddPanel.Value = objAddPanel.SerializeToXml<AddPanel>( ).ToBase64String( );
            }
            if ( e.CommandName == "Edit" )
            {
                Mode( e.CommandName.ToString( ), e.CommandArgument.ToString( ) );
            }
            if ( e.CommandName == "Copy" )
            {
                Mode( e.CommandName.ToString( ), e.CommandArgument.ToString( ) );
            }
            setSubmitValidation( );
        }

        protected void onItemCommanForAccess( object sender, RepeaterCommandEventArgs e )
        {
            AddPanel objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
            ServerValues val = objAddPanel.currentUIContentValueV;
            Access refA = new Access( );
            string refL = string.Empty;
            if ( e.CommandName == "Delete" )
            {
                foreach ( string l in val.accessUserRoleColl )
                {
                    if ( e.CommandArgument.Equals( l ) )
                    {
                        foreach ( Access a in val.Access )
                        {
                            if ( a.User.Equals( e.CommandArgument ) )
                            {
                                uiUsers.Items.Add( new ListItem( l, l ) );
                                refL = l;
                                refA = a;
                            }
                        }
                        val.refAccess.Add( refA );
                        val.Access.Remove( refA );
                    }
                }
                val.refaccessUserRoleColl.Add( refL );
                val.accessUserRoleColl.Remove( refL );
                accessRepeater.DataSource = val.Access;
                accessRepeater.DataBind( );
                summarize( objAddPanel.accessArg );
            }
            uiObjAddPanel.Value = objAddPanel.SerializeToXml<AddPanel>( ).ToBase64String( );
        }

        /**
            * On Page Load we are setting the <uiUpadate> panel which has the contrls where we fill server details to HIDDEN(invisible).
            * Setting the <uiuiAddServerRequest> panel which has the <AddServerRequest> button and other edit options to VISIBLE.
            * Gist : On Page load we only show the user the Add Request Button and Summay details and edit option of the servers if there exist any.
        **/
        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !Page.IsPostBack )
            {
                isChanged.Value = false.ToString( );
                if ( HttpContext.Current.Request.QueryString[ "ItemGuid" ] == null )
                {
                    PageLoadNewRequest( );
                    setSubmitValidation( );
                }
                else if ( HttpContext.Current.Request.QueryString[ "ItemGuid" ] == string.Empty )
                {
                    PageLoadNewRequest( );
                    setSubmitValidation( );
                }
                else
                {
                    try
                    {
                        Guid ItemGuid = HttpContext.Current.Request.QueryString[ "ItemGuid" ].ToGuid( );
                        StifelServerBuildDao dao = new StifelServerBuildDao( );
                        StifelVirtualServerRequest request = dao.getRequestFromListUsingGUID( ItemGuid );
                        if ( request != null )
                        {
                            uiLitValRequest.Text = request.Id.Value.ToString( );
                            Summary s = new Summary( );
                            VirtualServerCollectionClass v = request.getRequestInfoDeserialized( );
                            List<RepeaterBinding> summaryBinder = s.getSummary( v );
                            if ( request.getRequestStatus( ) == true )
                            {
                                AddPanel objAddPanel = new AddPanel( );
                                uiLitNotification.Text = "Saved And Can Be Edited";
                                myRepeater.DataSource = summaryBinder;
                                myRepeater.DataBind( );
                                objAddPanel.repeaterBinding = summaryBinder;
                                uiUpdate.Visible = false;
                                uiAddServerRequest.Visible = true;
                                uiObjAddPanel.Value = objAddPanel.SerializeToXml<AddPanel>( ).ToBase64String( );
                                setSubmitValidation( );
                            }
                            if ( request.getRequestStatus( ) == false )
                            {
                                if ( request.RequestApproved == false && request.RequestRejected == false )
                                {
                                    uiLitNotification.Text = "Submitted. Cannot Make Any Changes";
                                }
                                else if ( request.RequestApproved == true && request.RequestRejected == false )
                                {
                                    if ( request.RequestCompleted.HasValue )
                                    {
                                        if ( request.RequestCompleted.Value == true )
                                        {
                                            uiLitNotification.Text = "Servers Completed";
                                        }
                                        else
                                        {
                                            uiLitNotification.Text = "Request For Servers APPROVED";
                                        }
                                    }
                                    else
                                    {
                                        uiLitNotification.Text = "Request For Servers APPROVED";
                                    }
                                }
                                else
                                {
                                    uiLitNotification.Text = "Request For Servers REJECTED";
                                }
                                myRepeater.DataSource = summaryBinder;
                                myRepeater.DataBind( );
                                foreach ( RepeaterItem r in myRepeater.Items )
                                {
                                    ImageButton ie = ( ImageButton ) r.FindControl( "editButton" );
                                    ImageButton ic = ( ImageButton ) r.FindControl( "copyButton" );
                                    ImageButton id = ( ImageButton ) r.FindControl( "deleteButton" );
                                    ie.ImageUrl = "~/_layouts/com.stifel.ServerBuildMgmt/images/editg20x20grey.png";
                                    ic.ImageUrl = "~/_layouts/com.stifel.ServerBuildMgmt/images/copyblue20x20grey.png";
                                    id.ImageUrl = "~/_layouts/com.stifel.ServerBuildMgmt/images/redX20x20grey.png";
                                    ie.Enabled = false;
                                    ic.Enabled = false;
                                    id.Enabled = false;
                                }
                                uiBtnAddServerRequest.Enabled = false;
                                uiBtnSave.Enabled = false;
                                uiBtnSubmit.Enabled = false;
                                uiUpdate.Visible = false;
                                uiAddServerRequest.Visible = true;
                            }
                        }
                    }
                    catch ( Exception ex )
                    {
                        string errorMsg = ex.Message;
                        Page.Response.Redirect( getUrl( ) );
                        PageLoadNewRequest( );
                    }
                }
                isSomethingChanged( );
                setUrlReferrer( );
            }
        }

        protected void repeaterHardDrive_ItemDataBound( object sender, RepeaterItemEventArgs e )
        {
            AddPanel objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
            if ( e.Item.ItemType == ListItemType.Header )
            {
                Literal defalutDrive = ( Literal ) e.Item.FindControl( "DefaultDrive" );
                defalutDrive.Text = objAddPanel.dftDrive;
                Literal defaultName = ( Literal ) e.Item.FindControl( "DefaultName" );
                defaultName.Text = objAddPanel.dtfName;
                Literal defaultSize = ( Literal ) e.Item.FindControl( "DefaultSize" );
                defaultSize.Text = objAddPanel.dftSize.ToString( ) + "GB";
            }
        }

        /**
         * On clicking the Add Server Button it will display all the tabs where we could enter the new server details.
         * Creates a new instance to the <RepeaterBinding> class.
         * Add that purticular instance to <currentRepeaterBindingR> which will alsways have the current RepeaterBinding Instance.
         * Adds the current RepeaterBinding Instance to the <repeaterBinding> List.
        **/
        protected void uiBtnAddServerRequest_Click( object sender, EventArgs e )
        {
            SetRequestPanel( );
            AddPanel objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
            ServerValues v = new ServerValues( );

            objAddPanel.currentUIContentValueV = v;

            RepeaterBinding r = new RepeaterBinding( );
            objAddPanel.currentRepeaterBindingR = r;
            uiObjAddPanel.Value = objAddPanel.SerializeToXml<AddPanel>( ).ToBase64String( );
            setControlsToDefault( );
            load( );
            setDefaultButton( );
            uiSummaryDisasterRecovery.Controls.Clear( );
            uiSummaryAccess.Controls.Clear( );
            uiSummaryNotes.Controls.Clear( );
            uiSummaryServerRoles.Controls.Clear( );
            uiSummaryBackup.Controls.Clear( );
            //ClearAllPlaceHolders( );
            uiAddServerRequest.Visible = false;
            uiUpdate.Visible = true;
        }

        protected void uiBtnCancel_Click( object sender, EventArgs e )
        {
            Page.Response.Redirect( UrlReferrer );
        }

        protected void uiBtnSave_Click( object sender, EventArgs e )
        {
            SaveServers( );
        }

        protected void uiBtnSubmit_Click( object sender, EventArgs e )
        {
            SubmitServers( );
        }

        /**
         * On clicking cancle, removes the Current instance of <RepeaterBinding> class from the <repeaterBindings> List.
         * And then it will load the same page again which will call the <Page_Load> method.
        **/
        protected void uiCancelButton_Click( object sender, System.Web.UI.ImageClickEventArgs e )
        {
            //Removing the current instance from <repeaterBindings> List.
            uiNetworkConnectionOtherDescriptionMsg.Text = string.Empty;
            uiDisasterRecoveryDscMsg.Text = string.Empty;
            uiReplaceServerValidationMsg.Text = string.Empty;
            uiLblServerNameValidationMsg.Text = string.Empty;
            uiLblServerNameValidationMsg1.Text = string.Empty;
            uiLblBackupScheduleErrorMsg.Text = string.Empty;
            uiNcReasonErrorMsg.Text = string.Empty;
            uiLblOsReasonErrorMsg.Text = string.Empty;
            clearHardDriveErrorMsg( );

            if ( uiHiddenEditOrCopyModeFlag.Value.FromBase64String( ).Equals( "yes", StringComparison.InvariantCultureIgnoreCase ) )
            {
                uiHiddenEditOrCopyModeFlag.Value = "No".ToBase64String( );
            }

            AddPanel objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
            if ( ( objAddPanel.isEditMode == false ) && ( objAddPanel.isCopyMode == false ) )
            {
                uiLblServerNameValidationMsg.Text = string.Empty;
                uiUpdate.Visible = false;
                uiAddServerRequest.Visible = true;
            }
            else if ( objAddPanel.isCopyMode == true )
            {
                uiLblServerNameValidationMsg.Text = string.Empty;
                objAddPanel.isCopyMode = false;
                uiObjAddPanel.Value = objAddPanel.SerializeToXml<AddPanel>( ).ToBase64String( );
                uiUpdate.Visible = false;
                uiAddServerRequest.Visible = true;
            }
            else
            {
                uiLblServerNameValidationMsg.Text = string.Empty;
                objAddPanel.isEditMode = false;
                uiObjAddPanel.Value = objAddPanel.SerializeToXml<AddPanel>( ).ToBase64String( );
                uiUpdate.Visible = false;
                uiAddServerRequest.Visible = true;
            }
        }

        protected void uiDdlBackupHours_SelectedIndexChanged( object sender, EventArgs e )
        {
            summarize( );
        }

        protected void uiDdlLocation_SelectedIndexChanged( object sender, EventArgs e )
        {
            summarize( );
        }

        protected void uidropDownDrive_TextChanged( object sender, EventArgs e )
        {
            summarize( );
        }

        protected void uidropDownSize_TextChanged( object sender, EventArgs e )
        {
            summarize( );
        }

        protected void uiNavigationTab_Click( object sender, EventArgs e )
        {
            AddPanel objAddPanel;
            Button button = sender as Button;
            List<string> ambiguiousDriveNames = hardDriveValidation( );
            List<RepeaterItem> emptyHardDrives = hardDriveEmptyValidation( );
            if ( ( isNetworkConnectionDescriptionRequired( ) == true ) || ( isNCReasonValidated( ) == false ) )
            {
                setDefaultButton( "uiBtnNetworkConnections" );
                if ( isNetworkConnectionDescriptionRequired( ) == true )
                    uiNetworkConnectionOtherDescriptionMsg.Text = "Type Description Required";
                if ( isNCReasonValidated( ) == false )
                    uiNcReasonErrorMsg.Text = "Reason Required";
            }
            else if ( isDisasterRecoveryDscRequired( ) == true )
            {
                setDefaultButton( "uiBtnDisasterRecovery" );
                uiDisasterRecoveryDscMsg.Text = "Type Description Required";
            }
            else if ( isBackupValidated( ) == false )
            {
                //setToBackUpButton( );
                setDefaultButton( "uiBtnBackup" );
                uiLblBackupScheduleErrorMsg.Text = "Incomplete Backup Request";
            }
            else if ( ( ambiguiousDriveNames.Count != 0 ) || ( emptyHardDrives.Count != 0 ) )
            {
                setDefaultButton( uiBtnHardDrives.ID );
                if ( ambiguiousDriveNames.Count != 0 )
                {
                    hardDriveSetErrorMsg( ambiguiousDriveNames );
                }
                else if ( emptyHardDrives.Count != 0 )
                {
                    hardDriveSetErrorMsg( emptyHardDrives );
                }
                else
                {

                }
            }
            else if ( ( isServerNameAlreadyExits( ) == true ) || ( isServerNameHavingSpaces( ) == true ) || ( isReplaceExistingServerChecked( ) == true ) || ( isOsReasonValidated( ) == true ) )
            {
                setDefaultButton( "uiBtnServerBasics" );
                if ( isReplaceExistingServerChecked( ) == true )
                {
                    uiReplaceServerValidationMsg.Text = "Existing Server Name Required";
                }
                else
                {
                    uiReplaceServerValidationMsg.Text = string.Empty;
                }
                if ( isServerNameAlreadyExits( ) == true )
                {
                    uiLblServerNameValidationMsg1.Text = "Server Name Already Exists. Please Enter New Name.";
                }
                else
                {
                    uiLblServerNameValidationMsg1.Text = string.Empty;
                }
                if ( isServerNameHavingSpaces( ) == true )
                {
                    uiLblServerNameValidationMsg.Text = "Server Name Cannot Contain spaces";
                }
                else
                {
                    uiLblServerNameValidationMsg.Text = string.Empty;
                }
                if ( isOsReasonValidated( ) == true )
                {
                    uiLblOsReasonErrorMsg.Text = "Reason Required";
                }
                else
                {
                    uiLblOsReasonErrorMsg.Text = string.Empty;
                }
            }
            else
            {
                clearHardDriveErrorMsg( );
                uiNetworkConnectionOtherDescriptionMsg.Text = string.Empty;
                uiDisasterRecoveryDscMsg.Text = string.Empty;
                uiReplaceServerValidationMsg.Text = string.Empty;
                uiLblServerNameValidationMsg.Text = string.Empty;
                uiLblServerNameValidationMsg1.Text = string.Empty;
                uiLblBackupScheduleErrorMsg.Text = string.Empty;
                uiNcReasonErrorMsg.Text = string.Empty;
                uiLblOsReasonErrorMsg.Text = string.Empty;
                SerializableStifelOS p = uiValOperatingSystem.SelectedItem.Value.FromBase64String( ).DeserializeFromXml<SerializableStifelOS>( );
                if ( button != null )
                {
                    objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
                    string arg = button.CommandArgument;
                    objAddPanel.args = arg;
                    uiObjAddPanel.Value = objAddPanel.SerializeToXml<AddPanel>( ).ToBase64String( );
                    LoadClass lc = uiHiddenLoadC.Value.FromBase64String( ).DeserializeFromXml<LoadClass>( );
                    if ( uiHiddenEditOrCopyModeFlag.Value.FromBase64String( ).Equals( "yes", StringComparison.InvariantCultureIgnoreCase ) )
                    {
                        LoadClass lcEditOrCopy = uiHiddenLoadCForEditAndCopy.Value.FromBase64String( ).DeserializeFromXml<LoadClass>( );
                        ServerValues v = uiHiddenCurrentRequestValues.Value.FromBase64String( ).DeserializeFromXml<ServerValues>( );
                        if ( button.ID == uiBtnNetworkConnections.ID )
                        {
                            if ( lcEditOrCopy.isNetworkConnectionActivated == false )
                            {
                                LoadNetworkConnectionsViewForEditAndCopy( v, p );
                                lcEditOrCopy.isNetworkConnectionActivated = true;
                            }
                        }
                        if ( button.ID == uiBtnDisasterRecovery.ID )
                        {
                            if ( lcEditOrCopy.isDisasterRecovaryActivated == false )
                            {
                                LoadDisasterRecoveryViewForEditAndCopy( v );
                                lcEditOrCopy.isDisasterRecovaryActivated = true;
                            }
                        }
                        if ( button.ID == uiBtnServerRoles.ID )
                        {
                            if ( lcEditOrCopy.isServerRolesActivated == false )
                            {
                                LoadServerRolesViewForEditAndCopy( p, v );
                                lcEditOrCopy.isServerRolesActivated = true;
                            }
                        }
                        if ( button.ID == uiBtnAccess.ID )
                        {
                            if ( lcEditOrCopy.isServerRolesActivated == false )
                            {
                                //Setting Values To UI from Roles In Access.

                                #region Setting Values To UI from Roles In Access.

                                uiRolesForAccess_Load( );

                                #endregion

                                lcEditOrCopy.isAccessActivated = true;
                            }
                        }
                        if ( button.ID == uiBtnBackup.ID )
                        {
                            if ( lcEditOrCopy.isBackupActivated == false )
                            {
                                //Setting Values To UI from Backup Schedule

                                #region Setting Values To UI from Backup Schedule

                                setBackupSchedule( v.BackupSchedule );

                                #endregion

                                lcEditOrCopy.isBackupActivated = true;
                            }
                        }
                        uiHiddenLoadCForEditAndCopy.Value = lcEditOrCopy.SerializeToXml<LoadClass>( ).ToBase64String( );
                    }
                    else
                    {
                        if ( button.ID == uiBtnServerRoles.ID )
                        {
                            if ( lc.isServerRolesActivated == false )
                            {
                                uiRoles_Load( p );
                                lc.isServerRolesActivated = true;
                            }
                        }
                        if ( button.ID == uiBtnNetworkConnections.ID )
                        {
                            if ( lc.isNetworkConnectionActivated == false )
                            {
                                uiValNetworkConnectionsNumber_Load( p );
                                uiValNetworkConnectionsType_Load( p );
                                lc.isNetworkConnectionActivated = true;
                            }
                        }
                        if ( button.ID == uiBtnDisasterRecovery.ID )
                        {
                            if ( lc.isDisasterRecovaryActivated == false )
                            {
                                setDisasterRecovey( );
                                uiValDisasterRecoveryReplicationType_Load( );
                                lc.isDisasterRecovaryActivated = true;
                            }
                        }
                        if ( button.ID == uiBtnAccess.ID )
                        {
                            if ( lc.isAccessActivated == false )
                            {
                                uiRolesForAccess_Load( );
                                uiUsers.Items.Clear( );
                                uiUsers.Items.Add( string.Empty );
                                SPContext.Current.Web.GetAllUsers( ).ForEach( u => uiUsers.Items.Add( new ListItem( u.Name, u.SerializeToXml<SPUserInfoExpanded>( ).ToBase64String( ) ) ) );
                                lc.isAccessActivated = true;
                            }
                        }
                        if ( button.ID == uiBtnBackup.ID )
                        {
                            if ( lc.isBackupActivated == false )
                            {
                                setBackupSchedule( );
                                lc.isBackupActivated = true;
                            }
                        }
                        uiHiddenLoadC.Value = lc.SerializeToXml<LoadClass>( ).ToBase64String( );
                    }
                    navigateToSelectedView( button );
                    if ( arg.Equals( uiAccess.ID, StringComparison.OrdinalIgnoreCase ) )
                    {
                        objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
                        objAddPanel.accessArg = arg;
                        uiObjAddPanel.Value = objAddPanel.SerializeToXml<AddPanel>( ).ToBase64String( );
                    }
                    summarize( arg );
                }
            }
        }

        /**
         * On clicking OK it first call for the summary of each tab.
         * Creates a new instance to <UIContantValues> class which has all the data filled by the user.
         * Saving that purticular instance to <currentUIContentValueV> whcih will always have the current instance of <UIContentValues> class.
         * We will get the current instance of <RepeaterBinding> class.
         * We will get the complete summary and set that <summary> of current instance.
         * We are setting all the current data filled to current instance of <currentUIContentValueV> which we created earlier using <setValues> method.
         * Now we are getting the current instance of <currentUIContentValueV> coverter to Base64String and saving it to <value> of current instance of <RepeaterBinding> class.
         * Gist: Every time we click Ok button it will save the current <summary> and <value> to the new instance of <RepeaterBinding> class which is already added to <repeaterBindings> List.
        **/
        protected void uiOkButton_Click( object sender, System.Web.UI.ImageClickEventArgs e )
        {
            AddPanel objAddPanel;
            objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
            List<string> ambiguiousDriveNames = hardDriveValidation( );
            List<RepeaterItem> emptyHardDrives = hardDriveEmptyValidation( );
            if ( ( isServerNameAlreadyExits( ) == true ) || ( isServerNameHavingSpaces( ) == true ) || ( isReplaceExistingServerChecked( ) == true ) || ( isOsReasonValidated( ) == true ) )
            {
                setDefaultButton( "uiBtnServerBasics" );
                if ( isReplaceExistingServerChecked( ) == true )
                {
                    uiReplaceServerValidationMsg.Text = "Existing Server Name Required";
                }
                else
                {
                    uiReplaceServerValidationMsg.Text = string.Empty;
                }
                if ( isServerNameAlreadyExits( ) == true )
                {
                    uiLblServerNameValidationMsg1.Text = "Server Name Already Exists. Please Enter New Name.";
                }
                else
                {
                    uiLblServerNameValidationMsg1.Text = string.Empty;
                }
                if ( isServerNameHavingSpaces( ) == true )
                {
                    uiLblServerNameValidationMsg.Text = "Server Name Cannot Contain spaces";
                }
                else
                {
                    uiLblServerNameValidationMsg.Text = string.Empty;
                }
                if ( isOsReasonValidated( ) == true )
                {
                    uiLblOsReasonErrorMsg.Text = "Reason Required";
                }
                else
                {
                    uiLblOsReasonErrorMsg.Text = string.Empty;
                }
            }
            else if ( ( ambiguiousDriveNames.Count != 0 ) || ( emptyHardDrives.Count != 0 ) )
            {
                setDefaultButton( uiBtnHardDrives.ID );
                if ( ambiguiousDriveNames.Count != 0 )
                {
                    hardDriveSetErrorMsg( ambiguiousDriveNames );
                }
                else if ( emptyHardDrives.Count != 0 )
                {
                    hardDriveSetErrorMsg( emptyHardDrives );
                }
                else
                {

                }
            }
            else if ( isDisasterRecoveryDscRequired( ) == true )
            {
                setDefaultButton( "uiBtnDisasterRecovery" );
                uiDisasterRecoveryDscMsg.Text = "Type Description Required";
            }
            else if ( ( isNetworkConnectionDescriptionRequired( ) == true ) || ( isNCReasonValidated( ) == false ) )
            {
                setDefaultButton( "uiBtnNetworkConnections" );
                if ( isNetworkConnectionDescriptionRequired( ) == true )
                    uiNetworkConnectionOtherDescriptionMsg.Text = "Type Description Required";
                if ( isNCReasonValidated( ) == false )
                    uiNcReasonErrorMsg.Text = "Reason Required";
            }
            else if ( isBackupValidated( ) == false )
            {
                setDefaultButton( "uiBtnBackup" );
                uiLblBackupScheduleErrorMsg.Text = "Incomplete Backup Request";
            }
            else
            {
                uiLblServerNameValidationMsg.Text = string.Empty;
                uiLblServerNameValidationMsg1.Text = string.Empty;
                uiLblBackupScheduleErrorMsg.Text = string.Empty;
                uiNcReasonErrorMsg.Text = string.Empty;
                uiLblOsReasonErrorMsg.Text = string.Empty;
                clearHardDriveErrorMsg( );
                //SummarizeInOkButton( );

                if ( objAddPanel.isCopyMode == true )
                {
                    LoadTabsOnClickOkButtonForEditAndCopy( );
                    SummarizeInOkButton( );
                    objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
                    objAddPanel.currentUIContentValueV.refAccess.Clear( );
                    objAddPanel.currentUIContentValueV.refaccessUserRoleColl.Clear( );
                    objAddPanel.isCopyMode = false;
                    objAddPanel.currentRepeaterBindingR.summary = panelsToHtmlString( getPanels( ) );
                    setValues( objAddPanel.currentUIContentValueV );
                    objAddPanel.currentRepeaterBindingR.value = getValueAsBase64String( objAddPanel.currentUIContentValueV );
                    objAddPanel.repeaterBinding.Add( objAddPanel.currentRepeaterBindingR );
                    myRepeater.DataSource = objAddPanel.repeaterBinding;
                    myRepeater.DataBind( );
                    uiObjAddPanel.Value = objAddPanel.SerializeToXml<AddPanel>( ).ToBase64String( );
                    uiUpdate.Visible = false;
                    uiAddServerRequest.Visible = true;
                }
                else if ( objAddPanel.isEditMode == true )
                {
                    LoadTabsOnClickOkButtonForEditAndCopy( );
                    SummarizeInOkButton( );
                    objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
                    for ( int i = 0; i < objAddPanel.repeaterBinding.Count; i++ )
                    {
                        if ( objAddPanel.currentRepeaterBindingR.value.FromBase64String( ).Equals( objAddPanel.repeaterBinding[ i ].value.FromBase64String( ) ) )
                        {
                            objAddPanel.currentUIContentValueV.refAccess.Clear( );
                            objAddPanel.currentUIContentValueV.refaccessUserRoleColl.Clear( );
                            objAddPanel.isEditMode = false;
                            objAddPanel.repeaterBinding[ i ].summary = panelsToHtmlString( getPanels( ) );
                            setValues( objAddPanel.currentUIContentValueV );
                            objAddPanel.repeaterBinding[ i ].value = getValueAsBase64String( objAddPanel.currentUIContentValueV );
                            myRepeater.DataSource = objAddPanel.repeaterBinding;
                            myRepeater.DataBind( );
                            uiObjAddPanel.Value = objAddPanel.SerializeToXml<AddPanel>( ).ToBase64String( );
                            uiUpdate.Visible = false;
                            uiAddServerRequest.Visible = true;
                        }
                    }
                }
                else
                {
                    LoadTabsOnClickOkButton( );
                    SummarizeInOkButton( );
                    objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
                    objAddPanel.currentUIContentValueV.refAccess.Clear( );
                    objAddPanel.currentUIContentValueV.refaccessUserRoleColl.Clear( );
                    objAddPanel.isEditMode = false;
                    objAddPanel.currentRepeaterBindingR.summary = panelsToHtmlString( getPanels( ) );
                    setValues( objAddPanel.currentUIContentValueV );
                    objAddPanel.currentRepeaterBindingR.value = getValueAsBase64String( objAddPanel.currentUIContentValueV );
                    objAddPanel.repeaterBinding.Add( objAddPanel.currentRepeaterBindingR );

                    myRepeater.DataSource = objAddPanel.repeaterBinding;
                    myRepeater.DataBind( );

                    uiObjAddPanel.Value = objAddPanel.SerializeToXml<AddPanel>( ).ToBase64String( );
                    uiUpdate.Visible = false;
                    uiAddServerRequest.Visible = true;
                }
            }
            isChanged.Value = true.ToString( );
            isSomethingChanged( );
            setSubmitValidation( );
        }

        protected void uiRoles_TextChanged( object sender, EventArgs e )
        {
            ListItem uiUserItem = uiUsers.SelectedItem;
            ListItem uiRolesItem = uiRoles.SelectedItem;
            if ( ( uiUserItem.Text == string.Empty ) && ( uiRolesItem.Text == string.Empty ) )
            {
                AddUserAndRole.Visible = true;
            }
            else if ( ( uiUserItem.Text != string.Empty ) && ( uiRolesItem.Text == string.Empty ) )
            {
                AddUserAndRole.Enabled = true;
                AddUserAndRole.Visible = false;
            }
            else if ( ( uiUserItem.Text == string.Empty ) && ( uiRolesItem.Text != string.Empty ) )
            {
                AddUserAndRole.Enabled = true;
                AddUserAndRole.Visible = false;
            }
            else
            {
                AddUserAndRole.Enabled = true;
                AddUserAndRole.Visible = true;
            }
            summarize( );
        }

        protected void uiServerRoles_ItemDataBound( object sender, RepeaterItemEventArgs e )
        {
            if ( ( e.Item.ItemType == ListItemType.Item ) || ( e.Item.ItemType == ListItemType.AlternatingItem ) )
            {
                Label uiLblServerRoles = ( Label ) e.Item.FindControl( "uiLblServerRole" );
                CheckBox uiCServerRoles = ( CheckBox ) e.Item.FindControl( "uiServerRoles" );
                CheckBoxList uiCLSubRoles = ( CheckBoxList ) e.Item.FindControl( "uiSubRoles" );
                Label uiSpacing = ( Label ) e.Item.FindControl( "uiSpacing" );
                HiddenField uiHfSubRolesAsString = ( HiddenField ) e.Item.FindControl( "uiHiddenFieldSubRolesString" );
                List<string> subRoles = getListFromSemiColonConcatnatedString( uiHfSubRolesAsString.Value.FromBase64String( ).ToString( ) );
                if ( ( subRoles.Count != 0 ) && ( subRoles != null ) )
                {
                    uiCServerRoles.Enabled = false;
                    uiCServerRoles.Visible = false;
                    uiLblServerRoles.Visible = true;
                    uiSpacing.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    foreach ( string s in subRoles )
                    {
                        uiCLSubRoles.Items.Add( new ListItem( s.Trim(), s.Trim() ) );
                    }
                }
                else
                {
                    uiLblServerRoles.Visible = false;
                    uiCServerRoles.Visible = true;
                    uiCServerRoles.Enabled = true;
                }
            }
        }

        //protected void uiServerRolesCheckedChanged( object sender, EventArgs e )
        //{
        //    //ServerRoleValidation( );
        //    summarize( );
        //}
        protected void uiUsers_TextChanged( object sender, EventArgs e )
        {
            AddPanel objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
            ListItem uiUserItem = uiUsers.SelectedItem;
            ListItem uiRolesItem = uiRoles.SelectedItem;
            if ( ( uiUserItem.Text == string.Empty ) && ( uiRolesItem.Text == string.Empty ) )
            {
                AddUserAndRole.Visible = true;
            }
            else if ( ( uiUserItem.Text != string.Empty ) && ( uiRolesItem.Text == string.Empty ) )
            {
                AddUserAndRole.Enabled = true;
                AddUserAndRole.Visible = false;
            }
            else if ( ( uiUserItem.Text == string.Empty ) && ( uiRolesItem.Text != string.Empty ) )
            {
                AddUserAndRole.Enabled = true;
                AddUserAndRole.Visible = false;
            }
            else
            {
                AddUserAndRole.Visible = true;
                AddUserAndRole.Enabled = true;
            }
            summarize( );
        }

        protected void uiValCPUs_TextChanged( object sender, EventArgs e )
        {
            summarize( );
        }

        protected void uiValDisasterRecoveryReplicationType_TextChanged( object sender, EventArgs e )
        {
            StifelDRReplicationType rt = uiValDisasterRecoveryReplicationType.SelectedItem.Value.FromBase64String( ).DeserializeFromXml<StifelDRReplicationType>( );
            if ( rt.DRReplicationTypeDescrRqd.Value == true )
            {
                uiValDisasterRecoveryReplicationTypeOther.Visible = true;
                uiLblDisasterRecoveryReplicationTypeOther.Visible = true;
                uiDisasterRecoveryFlag.Visible = true;
            }
            else
            {
                uiValDisasterRecoveryReplicationTypeOther.Visible = false;
                uiLblDisasterRecoveryReplicationTypeOther.Visible = false;
                uiDisasterRecoveryFlag.Visible = false;
                uiDisasterRecoveryDscMsg.Text = string.Empty;
            }
            summarize( );
        }

        protected void uiValDisasterRecoveryRequested_CheckedChanged( object sender, EventArgs e )
        {
            setDisasterRecovey( );
            summarize( uiBtnDisasterRecovery.CommandArgument.ToString( ) );
        }

        protected void uiValMemory_TextChanged( object sender, EventArgs e )
        {
            summarize( );
        }

        protected void uiValNetworkConnectionsNumber_TextChanged( object sender, EventArgs e )
        {
            SetNCReasonVisibleOrInvisible( );
            summarize( );
        }

        protected void uiValNetworkConnectionsType_TextChanged( object sender, EventArgs e )
        {
            AddPanel objAddPanel = uiObjAddPanel.Value.FromBase64String( ).DeserializeFromXml<AddPanel>( );
            StifelNetworkConnectionTypeSerializable ntSerializable = uiValNetworkConnectionsType.SelectedItem.Value.FromBase64String( ).DeserializeFromXml<StifelNetworkConnectionTypeSerializable>( );
            if ( ntSerializable.ncTypeDscReq == true )
            {
                uiLblNetworkConnectionTypeOther.Visible = true;
                uiValNetworkConnectionTypeOther.Visible = true;
                uiNetworkConnectionDescriptionFlag.Visible = true;
            }
            else
            {
                uiLblNetworkConnectionTypeOther.Visible = false;
                uiValNetworkConnectionTypeOther.Visible = false;
                uiNetworkConnectionDescriptionFlag.Visible = false;
                uiNetworkConnectionOtherDescriptionMsg.Text = string.Empty;
            }
            summarize( );
        }

        protected void uiValOperatingSystem_TextChanged( object sender, EventArgs e )
        {
            ListItem item = uiValOperatingSystem.SelectedItem;
            string s = item.Value;
            SerializableStifelOS o = s.FromBase64String( ).DeserializeFromXml<SerializableStifelOS>( );
            if ( o.LegacyOS.HasValue )
            {
                if ( o.LegacyOS.Value == true )
                {
                    setOsReasonVisible( );
                }
            }
            if ( ( !o.LegacyOS.HasValue ) || ( o.LegacyOS.Value == false ) )
            {
                setOsReasonHide( );
            }
            uiValMemory_Load( o );
            uiValCPUs_Load( o );
            repeaterHardDrive_Load( o );
            //uiRoles_Load( o );
            //uiValNetworkConnectionsNumber_Load( o );
            //uiValNetworkConnectionsType_Load( o );
            summarize( );
        }

        protected void uiValReplaceExisting_CheckedChanged( object sender, EventArgs e )
        {
            setReplceExistingServer( );
            summarize( );
        }

        #endregion Methods
    }
}